package com.macoli.mock_server.httpserver.beans


enum class HTTP_METHOD {
    GET ,
    POST;
}

class RouteItem {
    var method : HTTP_METHOD
    var uri : String
    private var destnationList : ArrayList<Destnation> = ArrayList()

    constructor(method: HTTP_METHOD , uri:String , defaultPath : String , static : Boolean) {
        this.method = method
        this.uri = uri
        addDestnation(true , defaultPath , static ) {
            true
        }
    }

    fun addDestnation(default : Boolean , destnationPath : String , static: Boolean ,
                      condition : (params : Map<String, List<String>>) -> Boolean) {
        val destnation = Destnation()
        destnation.predicate = condition
        destnation.default = default
        destnation.destnation = destnationPath
        destnation.static = static
        destnationList.add(destnation)
    }

    fun matchCondition(params : Map<String, List<String>>) : Destnation{
        var defaultDestnation : Destnation? = null
        for (destnation in destnationList) {
            if (destnation.default) {
                defaultDestnation = destnation
                continue
            }
            if (destnation.predicate(params)) {
                return destnation
            }
        }
        return defaultDestnation!!
    }
}